package com.sls.mall.order.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.sls.common.to.mq.SeckillOrderTo;
import com.sls.mall.order.service.OmsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Service
public class SeckillOrderListener {

    @Autowired
    OmsOrderService orderService;

    /**
     * 监听秒杀订单消息
     * @param entity
     */
    @RabbitHandler
    public void listener(SeckillOrderTo entity, Channel channel, Message message) throws IOException {
        try{
            log.info("秒杀订单创建监听内容："+ JSON.toJSONString(entity));
            orderService.createSeckillOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e){
            log.error("秒杀订单消息处理异常：", e);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
